Thread: [C+Win32]Why is my code not compiling

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    31

    [C+Win32]Why is my code not compiling

    hello guys,

    i don't know whats wrong with this code of mine.

    Code:
    #include <windows.h>
    #include <wchar.h>
    
    #define BUF_LEN 10;
    
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    void CreateLabels(HWND);
    
    
    HWND hwndSta1;
    HWND hwndSta2;
    
    int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int CmdShow){
        HWND hwnd;
        MSG msg;
    
        WNDCLASSW wc = {0};
    
        wc.lpszClassName = L"Moving";
        wc.hInstance = hInstance;
        wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
        wc.lpfnWndProc = WndProc;
        wc.hCursor = LoadCursor(0, IDC_ARROW);
    
        RegisterClassW(&wc);
        hwnd = CreateWindowW(wc.lpszClassName, L"Window", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 150, 150, 250, 180, 0, 0, hInstance, 0);
    
        while(GetMessage(&msg, NULL, 0,0)){
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    
        return (int)msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
        wchar_t buf[10];
        RECT rect;
    
        switch(msg){
            case WM_CREATE:
                CreateLabels(hwnd);
                break;
    
            case WM_MOVE:
                GetWindowRect(hwnd, &rect);
    
                StringCbPrintfW(buf, BUF_LEN, L"%ld", rect.left);
                SetWindowTextW(hwndSta1, buf);
    
                StringCbPrintfW(buf, BUF_LEN, L"%ld", rect.top);
                SetWindowTextW(hwndSta2, buf);
    
                break;
            case WM_DESTROY:
                PostQuitMessage(0);
                break;
        }
    
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    
    
    void CreateLabels(HWND hwnd){
        CreateWindowW(L"static", L"x: ", WS_CHILD | WS_VISIBLE, 10, 10, 25, 25, hwnd, (HMENU)1, NULL, NULL);
        hwndSta1 = CreateWindowW(L"static", L"150", WS_CHILD | WS_VISIBLE, 40, 10, 55, 25, hwnd, (HMENU)2, NULL, NULL);
    
        CreateWindowW(L"static", L"y: ", WS_CHILD | WS_VISIBLE, 10, 30, 25, 25, hwnd, (HMENU)3, NULL, NULL);
        hwndSta2 = CreateWindowW(L"static", L"150", WS_CHILD | WS_VISIBLE, 40, 30, 25, 25, hwnd, (HMENU)4, NULL, NULL );
    }
    when i tried to compile i got error

    Building winui.obj.
    C:\Users\xx\Documents\Pelles C Projects\winUI\winui.c(49): warning #2018: Undeclared function 'StringCbPrintfW' (did you mean 'StringFromIID'?); assuming 'extern' returning 'int'.
    C:\Users\xx\Documents\Pelles C Projects\winUI\winui.c(49): error #2001: Syntax error: expected ')' but found ';'.
    C:\Users\xx\Documents\Pelles C Projects\winUI\winui.c(49): error #2061: Invalid statement termination.
    C:\Users\xx\Documents\Pelles C Projects\winUI\winui.c(49): error #2001: Syntax error: expected ';' but found ')'.
    C:\Users\xx\Documents\Pelles C Projects\winUI\winui.c(49): error #2061: Invalid statement termination.
    C:\Users\xx\Documents\Pelles C Projects\winUI\winui.c(52): error #2001: Syntax error: expected ')' but found ';'.
    C:\Users\xx\Documents\Pelles C Projects\winUI\winui.c(52): error #2061: Invalid statement termination.
    C:\Users\xx\Documents\Pelles C Projects\winUI\winui.c(52): error #2001: Syntax error: expected ';' but found ')'.
    C:\Users\xx\Documents\Pelles C Projects\winUI\winui.c(52): error #2061: Invalid statement termination.
    *** Error code: 1 ***
    Done.

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,113
    This question would be better posted to the Windows Programming forum on this site.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > #define BUF_LEN 10;
    Putting ; on the end of #defines is a weird thing to do.

    Because this
    > StringCbPrintfW(buf, BUF_LEN, L"%ld", rect.left);
    Becomes this
    > StringCbPrintfW(buf, 10;, L"%ld", rect.left);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jul 2017
    Posts
    4
    May I ask an off-topic question?

    What is the best source for studying Windows programming with C/C++?
    I've searched Amazon and all the books I could find are very dated ... is there any new book on this subject?

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,113
    Did you ask on the Windows Programming forum? I would think they could give you the best recommendation.

  6. #6
    Registered User
    Join Date
    May 2017
    Posts
    31
    Quote Originally Posted by Salem View Post
    > #define BUF_LEN 10;
    Putting ; on the end of #defines is a weird thing to do.

    Because this
    > StringCbPrintfW(buf, BUF_LEN, L"%ld", rect.left);
    Becomes this
    > StringCbPrintfW(buf, 10;, L"%ld", rect.left);
    i just made this change to the code. the output is

    Code:
    Building winui.obj.
    C:\Users\xx\Documents\Pelles C Projects\winUI\winui.c(49): warning #2018: Undeclared function 'StringCbPrintfW' (did you mean 'StringFromIID'?); assuming 'extern' returning 'int'.
    Building winUI.exe.
    POLINK: error: Unresolved external symbol '_StringCbPrintfW'.
    POLINK: fatal error: 1 unresolved external(s).
    *** Error code: 1 ***
    Done.

  7. #7
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    You need to include Strsafe.h for StringCbPrintfW

    @Ufuk,
    the Windows API has not changed so much over the last 2 decades. It's mainly a C style interface.
    Charles Petzold's book is still relevant.
    Programming Microsoft Windows: Charles Petzold: 9780072850581: Amazon.com: Books

  8. #8
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    138
    Quote Originally Posted by thagulf2017 View Post
    i just made this change to the code. the output is

    Code:
    Building winui.obj.
    C:\Users\xx\Documents\Pelles C Projects\winUI\winui.c(49): warning #2018: Undeclared function 'StringCbPrintfW' (did you mean 'StringFromIID'?); assuming 'extern' returning 'int'.
    Building winUI.exe.
    POLINK: error: Unresolved external symbol '_StringCbPrintfW'.
    POLINK: fatal error: 1 unresolved external(s).
    *** Error code: 1 ***
    Done.
    Hi,

    Instead of using Pelles C , I suggest you to use one of the following compilers for Windows OS:


    • MS Visual Studio, commercial and AFAIK only supports C90.
    • Clang with different IDE's like Geany or Code::Blocks.
    • Mingw, a port of GCC.

  9. #9
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,113
    Quote Originally Posted by ordak View Post
    Instead of using Pelles C , I suggest you to use one of the following compilers for Windows OS:
    • MS Visual Studio, commercial and AFAIK only supports C90.
    • Clang with different IDE's like Geany or Code::Blocks.
    • Mingw, a port of GCC.
    MS Visual Studio uses only as much of C99 & possibly C11 as needed to support their implementation of C++.

    Although I have never used Pelles C, I have heard good things about it, and would recommend it over any MS compiler for C. Pelles is supposedly C99 & C11 compliant.

    Obviously, IMHO, the best compilers for Windows that are gcc & clang, and can be easily installed through MinGW64, or Cygwin.

    Also, I am not a big fan of IDE's. I prefer to do my compiling from the command line, or through emacs. (No, not starting a flame war!) ;^)

    I think it teaches the beginning user better. An IDE just gets in my way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling x64 code in VC++ 6.0?
    By cpjust in forum Windows Programming
    Replies: 7
    Last Post: 07-29-2008, 09:36 AM
  2. compiling c code
    By MadCow257 in forum C++ Programming
    Replies: 2
    Last Post: 02-16-2006, 09:26 AM
  3. compiling win32 apps under linux?
    By talz13 in forum Linux Programming
    Replies: 11
    Last Post: 02-07-2005, 12:43 PM
  4. compiling and executing issue with lcc win32
    By GanglyLamb in forum C Programming
    Replies: 10
    Last Post: 12-22-2004, 02:24 PM
  5. Win32 App not compiling???
    By grmoo in forum C++ Programming
    Replies: 10
    Last Post: 09-13-2002, 07:39 AM

Tags for this Thread